package cn.tedu.v3;

import java.io.*;
import java.net.BindException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.Scanner;

/**
 * 多人聊天室 Version 3
 * Server服务器：找到CS多线程类
 *  读入放入线程中
 *  写出直接写在run方法
 */
public class Server {
    private ServerSocket serverSocket;
    BufferedReader br=null;
    public Server() {
        try {
            serverSocket = new ServerSocket(9908);
        } catch (BindException e) {//BindException    端口号已被占用
            System.out.println("请检查端口号，被占用~");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void start() {
        Socket socket = null;
        String ip=null;
        try {
            while (true) {
                System.out.println("等待客户端连接");
                    socket = serverSocket.accept();
                 ip = socket.getInetAddress().getHostAddress();
                System.out.println("客户端" + ip + "连接上了");

                new Thread(new CS(socket)).start();

            }
        } catch (IOException e) {
            System.out.println("客户端连接失败了");
        } finally {
            if (serverSocket!=null&&serverSocket.isClosed()){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    System.out.println("服务器关闭失败~~~");
                }
                System.out.println("服务器关闭成功~Over");
            }
        }
    }
    public static void main(String[] args) {
        Server server = new Server();
        server.start();
    }

    private class CS implements Runnable {
        private Socket socket;
        private String ip;
        public CS(Socket s){
            this.socket=s;
            ip=socket.getInetAddress().getHostAddress();
        }
        public void run() {
            PrintWriter pw=null;
            // br = null;
            try {
              //1.2 读入操作
                BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF8"));
              //1.1 写出操作
                OutputStream os= socket.getOutputStream();
                OutputStreamWriter osw=new OutputStreamWriter(os,"UTF8");
                BufferedWriter bw=new BufferedWriter(osw);
                pw=new PrintWriter(bw,true);
              //1.3 创建一个线程----Lambda---读入功能--存入到线程中去
                new Thread(()->{
              //1.4 线程中--创建变量--接受客户端发来的文本
                    try {
                        String message;
                        while ((message= br.readLine())!=null) { //读入的客户端的
                            System.out.println(ip+"客户端说：" + message);//打印  客户端发来文本
                            System.out.print("服务器请输入：");
                        }
                    } catch (IOException e) {
                        System.out.println("客户端发来的消息断开了");
                    }
                }).start();
              //1.5 增加写出操作[修改目的：读入写出]
              //1.5.1 控制台卡住---输入内容
                Scanner input=new Scanner(System.in);
              //1.5.2 一直写出
                while(true){
                    System.out.println("服务器请输入：");
                    String sme= input.nextLine();
                    pw.println("服务器说："+sme);
                }
            } catch (UnsupportedEncodingException e) {
                System.out.println("请检查客户端/服务器端编码集：不一致");
            } catch (SocketException e) {
                System.out.println("客户端被重置");
            } catch (IOException e) {
                System.out.println("客户端连接失败了");
            } finally {
                //一定关闭：先关闭流，后关socket
                if (br!= null) ;
                try {
                    br.close();
                } catch (IOException e) {
                    System.out.println("读入流断开连接");
                }



            //2.关闭CS多线程类中--运行的socket通讯类
            if (socket!=null&&!socket.isClosed()) {
                System.out.println(ip + "客户端退出服务器链接~");
                try {
                    socket.close();
                } catch (IOException e) {
                    System.out.println(ip + "客户端退出链接异常~");
                }

            }
            }
        }
    }
}